package cn.com.taiji.oospub.wechat.service.impl;

import cn.com.taiji.common.pub.StringTools;
import cn.com.taiji.oospub.wechat.entity.CourseNutrition;
import cn.com.taiji.oospub.wechat.entity.Courses;
import cn.com.taiji.oospub.wechat.mapper.CourseNutritionMapper;
import cn.com.taiji.oospub.wechat.mapper.CoursesMapper;
import cn.com.taiji.oospub.wechat.model.NutritionModel;
import cn.com.taiji.oospub.wechat.service.ICourseNutritionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2020-10-28
 */
@Service
public class CourseNutritionServiceImpl extends ServiceImpl<CourseNutritionMapper, CourseNutrition> implements ICourseNutritionService {
    @Autowired
    private CourseNutritionMapper courseNutritionMapper;
    @Autowired
    private CoursesMapper coursesMapper;

    @Override
    public IPage queryPage(Page<CourseNutrition> page, CourseNutrition queryModel) {
        QueryWrapper<Courses> coursesWrapper = new QueryWrapper<>();
        if(StringTools.hasText(queryModel.getSchoolId())){
            coursesWrapper.eq("school_id",queryModel.getSchoolId());
        }
        if(StringTools.hasText(queryModel.getRecipesCode())){
            coursesWrapper.eq("pos_no",queryModel.getRecipesCode());
        }
        coursesWrapper.eq("is_delete", false);
        List<Courses> courses = coursesMapper.selectList(coursesWrapper);

        IPage courseNutritionPage = null;
        List<CourseNutrition> courseNutritionList =new ArrayList<CourseNutrition>();
        List<CourseNutrition> list 	 =new ArrayList<CourseNutrition>();

        for (Courses course: courses) {
            QueryWrapper<CourseNutrition> courseNutritionWrapper = new QueryWrapper<>();
            courseNutritionWrapper.eq("recipes_code", course.getPosNo());
            courseNutritionWrapper.eq("is_delete",false);
            courseNutritionPage = courseNutritionMapper.selectPage(page, courseNutritionWrapper);
            list =courseNutritionPage.getRecords();
            for (CourseNutrition courseNutrition : list) {
                if(!courseNutritionList.contains(courseNutrition)){
                    courseNutritionList.add(courseNutrition);
                }
            }

        }
        for (CourseNutrition courseNutrition : courseNutritionList) {
            if(courseNutrition.getIsDelete()) {
                courseNutrition.setIsDeleteStr("已删除");
            }
            if(!courseNutrition.getIsDelete()) {
                courseNutrition.setIsDeleteStr("未删除");
            }
        }
        courseNutritionPage.setRecords(courseNutritionList);



        return courseNutritionPage;
    }

    @Override
    public CourseNutrition findById(String id) {
        return courseNutritionMapper.selectById(id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void exportExcelDetail(Courses queryModel, HttpServletResponse response) {
        try {
            String fileName = ""; //文件名
            fileName = new String(("菜品营养表.xls").getBytes("gbk"),"iso8859-1");
            OutputStream os = response.getOutputStream();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-disposition","attachment; filename="+fileName);

            //大标题字体
            WritableCellFormat wcfTitle = new WritableCellFormat();
            WritableFont BigTitleFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
            wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfTitle.setFont(BigTitleFont);
            wcfTitle.setBorder(Border.ALL, BorderLineStyle.THIN);

            //表头字体
            WritableCellFormat wcfGrid = new WritableCellFormat();
            WritableFont gridTitleFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfGrid.setAlignment(jxl.format.Alignment.CENTRE);
            wcfGrid.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfGrid.setFont(gridTitleFont);
            wcfGrid.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfGrid.setWrap(true);

            //内容字体
            WritableCellFormat wcfL = new WritableCellFormat();
            wcfL.setAlignment(jxl.format.Alignment.LEFT);
            wcfL.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfL.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfL.setWrap(true);

            WritableCellFormat wcfLNumber = new WritableCellFormat();
            wcfLNumber.setAlignment(jxl.format.Alignment.RIGHT);
            wcfLNumber.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfLNumber.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfLNumber.setWrap(true);

            WritableWorkbook wb = Workbook.createWorkbook(os);
            WritableSheet sheet = wb.createSheet("订单信息表", 0);

            //大标题
            sheet.mergeCells(0, 0, 36, 0);
            sheet.setRowView(0, 1000);
            sheet.addCell(new Label(0, 0, "菜品营养表",wcfTitle));

            //表头
            sheet.addCell(new Label(0, 1, "菜品编号",wcfGrid));
            sheet.setColumnView(0, 14);
            sheet.addCell(new Label(1, 1, "菜品名称",wcfGrid));
            sheet.setColumnView(1, 14);
//    		sheet.addCell(new Label(0, 1, "菜品英文名称",wcfGrid));
//    		sheet.setColumnView(0, 14);

            sheet.addCell(new Label(2, 1, "能量数量",wcfGrid));
            sheet.setColumnView(2, 14);
            sheet.addCell(new Label(3, 1, "能量单位",wcfGrid));
            sheet.setColumnView(3, 14);


            sheet.addCell(new Label(4, 1, "碳水化合物数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(5, 1, "碳水化合物单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(6, 1, "钠数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(7, 1, "钠单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(8, 1, "蛋白质数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(9, 1, "蛋白质单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(10, 1, "脂肪数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(11, 1, "脂肪单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(12, 1, "可食部分数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(13, 1, "可食部分单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(14, 1, "水数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(15, 1, "水单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(16, 1, "膳食纤维数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(17, 1, "膳食纤维单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(18, 1, "维生素A数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(19, 1, "维生素A单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(20, 1, "维生素B1数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(21, 1, "维生素B1单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(22, 1, "维生素B2数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(23, 1, "维生素B2单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(24, 1, "烟酸数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(25, 1, "烟酸单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(26, 1, "维生素E数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(27, 1, "维生素E单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(28, 1, "钙数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(29, 1, "钙单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(30, 1, "铁数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(31, 1, "铁单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(32, 1, "维生素C数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(33, 1, "维生素C单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(34, 1, "胆固醇数量",wcfGrid));
            sheet.setColumnView(4, 14);
            sheet.addCell(new Label(35, 1, "胆固醇单位",wcfGrid));
            sheet.setColumnView(5, 14);

            sheet.addCell(new Label(36, 1, "营养标签",wcfGrid));
            sheet.setColumnView(4, 14);
            //根据条件查找
//    		List<CourseNutrition> courseNutritionList = this.courseNutritionDao.listQuery(queryModel);
//    		Set<String> set = new HashSet<String>();
//            List<String> recipeCodeList = new ArrayList<String>();
//            for (Iterator<CourseNutrition> iter = courseNutritionList.iterator(); iter.hasNext();) {
//            	CourseNutrition courseNutrition = (CourseNutrition) iter.next();
//                if (set.add(courseNutrition.getRecipesCode()))
//                    recipeCodeList.add(courseNutrition.getRecipesCode());
//            }
            queryModel.setIsDelete(false);
            List<Courses> coursesList = coursesMapper.selectList(new QueryWrapper<>(queryModel));
            Map<String,String> map =new HashMap<String,String>();
            for (Courses courses : coursesList) {
                map.put(courses.getPosNo(), courses.getPosNo());
            }
            int row = 2;
            int col = 0;
            for (String posNo : map.keySet()) {
                QueryWrapper<CourseNutrition> courseNutritionModel = new QueryWrapper<>();
                courseNutritionModel.eq("recipes_code",posNo);
                courseNutritionModel.eq("is_delete",false);
                List<CourseNutrition> courseNutritionList1 = this.courseNutritionMapper.selectList(courseNutritionModel);
                NutritionModel nutrionModel = new NutritionModel();

                for (CourseNutrition courseNutrition : courseNutritionList1) {

                    if(courseNutrition.getRecipesCode()!= null ){
                        nutrionModel.setRecipesCode(courseNutrition.getRecipesCode());
                    }
                    if(courseNutrition.getRecipesName()!= null ){
                        nutrionModel.setRecipesName(courseNutrition.getRecipesName());
                    }

                    if(courseNutrition.getNutritionCode()!= null && "NA01".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setNlNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setNlMeasure(courseNutrition.getNutritionMeasurement());
                    }

                    if(courseNutrition.getNutritionCode()!= null && "NA01".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setNlNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setNlMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA02".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setTshhwNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setTshhwMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA03".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setNaNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setNaMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA04".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setDbzNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setDbzMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA05".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setZfNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setZfMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA06".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setKsNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setKsMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA07".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setSfNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setSfMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA08".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setSsNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setSsMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA09".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setWssANumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setWssAMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA10".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setWssB1Number(courseNutrition.getQuantity().toString());
                        nutrionModel.setWssB1Measure(courseNutrition.getNutritionMeasurement());
                    }

                    if(courseNutrition.getNutritionCode()!= null && "NA11".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setWssB2Number(courseNutrition.getQuantity().toString());
                        nutrionModel.setWssB2Measure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA12".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setYsNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setYsMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA13".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setWssENumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setWssEMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA14".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setGaNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setGaMeasure(courseNutrition.getNutritionMeasurement());
                    }if(courseNutrition.getNutritionCode()!= null && "NA15".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setTieNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setTieMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA16".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setWssCNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setWssCMeasure(courseNutrition.getNutritionMeasurement());
                    }
                    if(courseNutrition.getNutritionCode()!= null && "NA17".equals(courseNutrition.getNutritionCode())){
                        nutrionModel.setDgcNumber(courseNutrition.getQuantity().toString());
                        nutrionModel.setDgcMeasure(courseNutrition.getNutritionMeasurement());
                    }


                }
                sheet.addCell(new Label(col++, row, nutrionModel.getRecipesCode()!=null?nutrionModel.getRecipesCode():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getRecipesName()!=null?nutrionModel.getRecipesName():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getNlNumber()!=null?nutrionModel.getNlNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getNlMeasure()!=null?nutrionModel.getNlMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getTshhwNumber()!=null?nutrionModel.getTshhwNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getTshhwMeasure()!=null?nutrionModel.getTshhwMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getNaNumber()!=null?nutrionModel.getNaNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getNaMeasure()!=null?nutrionModel.getNaMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getDbzNumber()!=null?nutrionModel.getDbzNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getDbzMeasure()!=null?nutrionModel.getDbzMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getZfNumber()!=null?nutrionModel.getZfNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getZfMeasure()!=null?nutrionModel.getZfMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getKsNumber()!=null?nutrionModel.getKsNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getKsMeasure()!=null?nutrionModel.getKsMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getSfNumber()!=null?nutrionModel.getSfNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getSfMeasure()!=null?nutrionModel.getSfMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getSsNumber()!=null?nutrionModel.getSsNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getSsMeasure()!=null?nutrionModel.getSsMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getWssANumber()!=null?nutrionModel.getWssANumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getWssAMeasure()!=null?nutrionModel.getWssAMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getWssB1Number()!=null?nutrionModel.getWssB1Number():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getWssB1Measure()!=null?nutrionModel.getWssB1Measure():"",wcfL));


                sheet.addCell(new Label(col++, row, nutrionModel.getWssB2Number()!=null?nutrionModel.getWssB2Number():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getWssB2Measure()!=null?nutrionModel.getWssB2Measure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getYsNumber()!=null?nutrionModel.getYsNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getYsMeasure()!=null?nutrionModel.getYsMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getWssENumber()!=null?nutrionModel.getWssENumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getWssEMeasure()!=null?nutrionModel.getWssEMeasure():"",wcfL));


                sheet.addCell(new Label(col++, row, nutrionModel.getGaNumber()!=null?nutrionModel.getGaNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getGaMeasure()!=null?nutrionModel.getGaMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getTieNumber()!=null?nutrionModel.getTieNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getTieMeasure()!=null?nutrionModel.getTieMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getWssCNumber()!=null?nutrionModel.getWssCNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getWssCMeasure()!=null?nutrionModel.getWssCMeasure():"",wcfL));

                sheet.addCell(new Label(col++, row, nutrionModel.getDgcNumber()!=null?nutrionModel.getDgcNumber():"",wcfL));
                sheet.addCell(new Label(col++, row, nutrionModel.getDgcMeasure()!=null?nutrionModel.getDgcMeasure():"",wcfL));


                Double nlNumber = 0.0;
                Double dbzNumber = 0.0;
                Double zfNumber = 0.0;
                if(nutrionModel.getNlNumber()!= null) {
                    nlNumber = Double.parseDouble(nutrionModel.getNlNumber());
                }
                if(nutrionModel.getDbzNumber()!= null) {
                    dbzNumber = Double.parseDouble(nutrionModel.getDbzNumber());
                }
                if(nutrionModel.getZfNumber()!= null) {
                    zfNumber = Double.parseDouble(nutrionModel.getZfNumber());
                }


                if(nlNumber<300&&nlNumber<10&&nlNumber<400){
                    sheet.addCell(new Label(col++, row, "eat Well",wcfL));

                }
                if(nlNumber<300){
                    sheet.addCell(new Label(col++, row, "低卡",wcfL));

                }
                if(nlNumber<10){
                    sheet.addCell(new Label(col++, row, "低脂",wcfL));

                }
                if(nlNumber<400){
                    sheet.addCell(new Label(col++, row, "低钠",wcfL));

                }

                row ++;
                col = 0;
            }


            wb.write();
            wb.close();
            os.flush();
            os.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<CourseNutrition> queryList(QueryWrapper<CourseNutrition> nutModel) {
        return courseNutritionMapper.selectList(nutModel);
    }
}
